﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PMS.BusinessFacade;
using PMS.EntityManage;
using PMS.Common;
using PMS.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;

using BusinessFacade;
using EntityManage;
using Microsoft.Reporting.WebForms;


public partial class Modules_Personal_PerfEvaluateOrgAllReport : System.Web.UI.Page
{
    IPerf_Personal_EvaluateHandler evaluateHandler = new Perf_Personal_EvaluateManage();
    IPerf_Personal_EvaluateDtlHandler evaluateDtlHandler = new Perf_Personal_EvaluateDtlManage();

    ISys_OrganizationHandler organizationHandler = new Sys_OrganizationManage();

    IPerf_Personal_TargetOwnerHandler targetOwnerHandler = new Perf_Personal_TargetOwnerManage();
    DataBase db = new DataBase();

    ISys_UserHandlerExt userHandlerExt = new Sys_UserManageExt();
    IPerf_Personal_EvaluateHandlerExt evaluateHandlerExt = new Perf_Personal_EvaluateManageExt();
    IPerf_Personal_FinalAssignHandler finalAssignHandler = new Perf_Personal_FinalAssignManage();

    IPerf_Personal_ApprovalHandler erfApprovalHandler = new Perf_Personal_ApprovalManage();
    IReportHandler rptHandler = new ReportManage();

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            FillDDl();
           // PreviewReport();
        }
    }

    private void FillDDl()
    {
        DataTable dt = rptHandler.GetQuarterList();
        ddlTitle.DataSource = dt;
        ddlTitle.DataTextField = "Title";
        ddlTitle.DataValueField = "Title";
        ddlTitle.DataBind();
        ddlTitle.Items.Insert(0, new ListItem(string.Empty,string.Empty));
    }
   
    #region Preview Report
    private void PreviewReport()
    {

         //当前用户所属部门(如果当前用户有借调部门就优先借调部门)
        int currUserOrgID = 0;
        if (Session["OrgID2"] != null)
        {
            if (Session["OrgID2"].ToString() != string.Empty && Session["OrgID2"].ToString() != "0")
            {
                currUserOrgID = int.Parse(Session["OrgID2"].ToString());
            }
            else if (Session["OrgID"] != null)
            {
                currUserOrgID = int.Parse(Session["OrgID"].ToString());
            }
        }

        Sys_OrganizationQuery orgQuery = organizationHandler.GetSys_OrganizationQuery(currUserOrgID);
        if (orgQuery != null)
        {

            //获取父部门
            Sys_OrganizationQuery orgPQuery = organizationHandler.GetSys_OrganizationQuery(orgQuery.Parent_ID);

            if (orgPQuery != null)
            {
                //根据父部门ID获取所有子部门ID
                string subOrgID = string.Empty;
                string sqlSub = @"select ID from Sys_Organization where Parent_ID=" + orgPQuery.ID;
                DataTable dtSub = db.RunSql(sqlSub);
                if (dtSub != null && dtSub.Rows.Count > 0)
                {
                    foreach (DataRow r in dtSub.Rows)
                    {
                        subOrgID += r["ID"].ToString() + ",";
                    }

                    //当前用户是属于工商所或机关才有评分
                    if (orgQuery.Type.ToString() == Enums.OrgType.Office.ToString("d") || (orgQuery.Type.ToString() == Enums.OrgType.ICU.ToString("d"))
                        || orgPQuery.Type.ToString() == Enums.OrgType.Office.ToString("d") || (orgPQuery.Type.ToString() == Enums.OrgType.ICU.ToString("d")))
                    {
                        string whereClause = " 1=1 ";
                        SqlParameter[] parms = null;
                        ProcessParms(ref whereClause, ref parms);

                        whereClause += " and (case when u.Org_ID2>0 then Org_ID2 else u.Org_ID end)in(" + subOrgID.TrimEnd(new char[] { ',' }) + ") ";

                        rptViewer.LocalReport.ReportPath = "Modules/Personal/rptPerfEvaluateOrgAll.rdlc";

                        rptViewer.LocalReport.DataSources.Clear();
                        DataTable dt = rptHandler.GetPerfEvaluateOrgAllData(whereClause, parms, " ID desc ");
                        if (dt != null && dt.Rows.Count > 0)
                        {
                            rptViewer.Visible = true;
                            lblNoRecord.Visible = false;
                            ReportDataSource datasource = new ReportDataSource("dsPerfEvaluateOrgAll_DataTable1", dt);
                            rptViewer.LocalReport.DataSources.Add(datasource);

                            ReportParameter[] parameters = new ReportParameter[3];
                            parameters[0] = new ReportParameter("DateFrom", Convert.ToDateTime(dt.Rows[0]["Begin_Time"].ToString()).ToString("yyyy-MM-dd"));
                            parameters[1] = new ReportParameter("DateTo", Convert.ToDateTime(dt.Rows[0]["End_Time"].ToString()).ToString("yyyy-MM-dd"));
                            parameters[2] = new ReportParameter("ReportTitle", dt.Rows[0]["Title"].ToString());
                            rptViewer.LocalReport.SetParameters(parameters);
                            rptViewer.LocalReport.Refresh();
                        }
                        else
                        {
                            rptViewer.Visible = false;
                            lblNoRecord.Visible = true;
                        }
                    }
                }
            }
        }
    }
    #endregion


    /// <summary>
    /// 条件参数处理
    /// </summary>
    /// <param name="whereClause"></param>
    /// <param name="parms"></param>
    private void ProcessParms(ref string whereClause, ref SqlParameter[] parms)
    {
        List<SqlParameter> lstParm = new List<SqlParameter>();


        if (ViewState["Title"] != null && ViewState["Title"].ToString() != string.Empty)
        {
            whereClause += " and Title =@Title ";
            lstParm.Add(new SqlParameter("@Title", ViewState["Title"].ToString()));
        }
    
        parms = lstParm.ToArray();
    }


    protected void btnSearch_Click(object sender, EventArgs e)
    {
        ViewState["Title"] = ddlTitle.SelectedValue;

      
        PreviewReport();
    }
}
